package service;

import com.alibaba.fastjson.JSON;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;


/**
 * @program: hw
 * @description: qiuheyi
 * @author: qhy
 * @create: 2020-08-10 10:17
 **/
public class UdpService {


//    private static String mp3Path = "/Users/qiuheyi/Downloads/死了都要爱.mp3";
//    private static MP3File mp3File;
//    private static final int START = 6;


    public static void main(String[] args) throws IOException, InterruptedException, InvalidAudioFrameException, TagException, ReadOnlyFileException {

        /**
         * 实现UDP协议通信的服务端
         */
        System.out.println("==========这是服务端=============");
        //创建DatagramSocket对象，用来监听7777端口，若有数据来传输，则接受
        DatagramSocket socket = new DatagramSocket(9526);
//        MulticastSocket socket=new MulticastSocket(9526);
        //创建字节数组
        byte[] by = new byte[1024];
        //创建DatagramPacket对象,用来接收数据，以字节数组形式接收
        DatagramPacket packet = new DatagramPacket(by, by.length);

        //接收客户端发送过来的数据
        socket.receive(packet);

        //获得用户发送的数据
        byte[] data = packet.getData();
        //获得客户端发送过来数据的长度以及端口
        int length = packet.getLength();
        int port = packet.getPort();

        System.out.println(data);

//        File sourceFile = new File("/Users/qiuheyi/Downloads/3uhiftau1j2.mp3-1");
//        AudioInputStream audioInputStream = null;
//        try {
//            audioInputStream = AudioSystem.getAudioInputStream(sourceFile);
//        } catch (UnsupportedAudioFileException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        AudioFormat format = audioInputStream.getFormat();
//        SourceDataLine auline = null;
//        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
//
//        try {
//            auline = (SourceDataLine) AudioSystem.getLine(info);
//            auline.open();
//        } catch (LineUnavailableException e) {
//            e.printStackTrace();
//        }
//        //允许某一个数据行执行数据i/o
//        auline.start();
//
//        //写出数据
//        int nBytesRead = 0;
//        byte[] abData = new byte[1024];
//        //从音频流读取指定的最大数量的数据字节，并将其放入给定的字节数组中。
//        try {
//            while (nBytesRead != -1) {
//                nBytesRead = audioInputStream.read(abData, 0, abData.length);
//                //通过此源数据行将数据写入混频器
//                if (nBytesRead >= 0)
//                    auline.write(abData, 0, nBytesRead);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            auline.drain();
//            auline.close();
//        }

        MP3File myFile = new MP3File("/Users/qiuheyi/Downloads/死了都要爱.mp3");
//      MP3AudioHeader header = myFile.getMP3AudioHeader();
//      DatagramSocket ds = new DatagramSocket(9000);
        DatagramPacket dp;
        int packetsize = 1024;
        double nosofpackets = Math.ceil(((int) myFile.getFile().length()) / packetsize);

        BufferedInputStream bis;
        bis = new BufferedInputStream(new FileInputStream(myFile.getFile()));
        for (double i = 0; i < nosofpackets + 1; i++) {
            byte[] mybytearray = new byte[packetsize];
            bis.read(mybytearray, 0, mybytearray.length);
            System.out.println("Packet:" + (i + 1));
            dp = new DatagramPacket(mybytearray, mybytearray.length, InetAddress.getByName("192.168.16.71"), 9528);
            socket.send(dp);
//            Thread.sleep(header.getBitRateAsNumber());
        }

//        for (int i = 1; i < 1052;i++){
//            InputStream is = new FileInputStream( new File("/Users/qiuheyi/Downloads/死了都要爱.mp3-"+i+".mp3"));
//            System.out.println(is.available());
//
//            byte[] b=new byte[is.available()];
//            is.read(b);
//
//            DatagramPacket dp=new DatagramPacket(b,0,b.length);
//
//            dp.setPort(9528);
//            dp.setAddress(InetAddress.getByName("192.168.16.71"));
//
//            socket.send(dp);
//        }
        socket.close();
    }


//    private static void getHead() {
//        try {
//            System.out.println("----------------Loading...Head-----------------");
//            mp3File = new MP3File(mp3Path);
//            MP3AudioHeader header = mp3File.getMP3AudioHeader();
//            System.out.println("时长: " + header.getTrackLength());
//            System.out.println("比特率: " + header.getBitRate());
//            System.out.println("音轨长度: " + header.getTrackLength());
//            System.out.println("格式: " + header.getFormat());
//            System.out.println("声道: " + header.getChannels());
//            System.out.println("采样率: " + header.getSampleRate());
//            System.out.println("MPEG: " + header.getMpegLayer());
//            System.out.println("MP3起始字节: " + header.getMp3StartByte());
//            System.out.println("精确的音轨长度: " + header.getPreciseTrackLength());
//        } catch (Exception e) {
//            System.out.println("没有获取到任何信息");
//        }
//    }
}
